package Finally;

import Test01.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSMS {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            showMenu();
            int option = sc.nextInt();
            switch (option) {
                case 1:
                    add(list, sc);
                    break;
                case 2:
                    delete(list, sc);
                    break;
                case 3:
                    update(list, sc);
                    break;
                case 4:
                    selectAll(list);
                    break;
                case 5:
                    System.out.println("退出");
                    System.out.println("谢谢使用,欢迎下次光临");
                    System.exit(0);//关闭JVM
                default:
                    System.out.println("目前系统尚不支持您输入的操作,请重新选择");
                    break;
            }
        }
    }

    private static void update(ArrayList<Student> list, Scanner sc) {
        if (list == null || list.isEmpty() || sc == null) {
            System.out.println("您输入的要修改的学号不存在,无法完成修改操作,请选择其它操作");
            return;
        }
        System.out.println("请输入要修改信息的学生的id:");
        String id = sc.next();
        Student stu = selcetById(list, id);
        if (stu == null) {
            System.out.println("您输入的要修改的学号不存在,无法完成修改操作,请选择其它操作");
            return;
        }
        System.out.println("请输入新的姓名:");
        String name = sc.next();
        System.out.println("请输入新的年龄:");
        String age = sc.next();
        System.out.println("请输入新的地址:");
        String address = sc.next();
        stu.setName(name);
        stu.setAge(age);
        stu.setAddress(address);
        System.out.println("修改成功");


    }

    private static void delete(ArrayList<Student> list, Scanner sc) {
        if (list == null || list.isEmpty() || sc == null) {
            System.out.println("先添加,再查看");
            return;
        }
        System.out.println("请输入要修改信息的学生的id:");
        String id = sc.next();
        Student stu = selcetById(list, id);
        if (stu == null) {
            System.out.println("您输入的要删除的学号不存在,无法完成删除操作,请选择其它操作");
            return;
        }
        list.remove(stu);
        System.out.println("删除成功");


    }

    private static void selectAll(ArrayList<Student> list) {
        if (list == null || list.isEmpty()) {
            System.out.println("先添加,再查看");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            String name = stu.getName();
            String age = stu.getAge();
            String address = stu.getAddress();
            System.out.println(id + "\t" + name + "\t" + age + "\t" + address);
        }
    }

    /**
     * 添加学生
     *
     * @param list
     * @param sc
     */
    private static void add(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入学号:");
        String id = sc.next();
        while (true) {
            Student stu = selcetById(list, id);
            if (stu != null) {
                System.out.println("您输入的id值已经被使用,请重新录入");
                id = sc.next();
            } else {
                break;

            }
        }
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        String age = sc.next();
        System.out.println("请输入地址:");
        String address = sc.next();
        list.add(new Student(id, name, age, address));
        System.out.println("添加成功");
    }

    private static Student selcetById(ArrayList<Student> list, String id) {
        if (list == null || list.isEmpty() || id == null) {
            return null;
        }
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getSid().equals(id)) {
                return stu;
            }
        }
        return null;

    }

    private static void showMenu() {
        System.out.println("--------欢迎来到学生管理系统--------");
        System.out.println("1 添加学生");
        System.out.println("2 删除学生");
        System.out.println("3 修改学生");
        System.out.println("4 查看所有学生");
        System.out.println("5 退出");
        System.out.println("请输入你的选择");
    }
}
